using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using Toenda.Foundation.Net;

namespace Toenda.Foundation.Utility {
	/// <summary>
	/// Class Converter
	/// </summary>
	public static class Converter {
		/// <summary>
		/// Get the date from a timestamp filename
		/// </summary>
		/// <param name="text">The filename.</param>
		/// <returns></returns>
		public static DateTime ConvertTimestampFileNameToDateTime(string text) {
			bool b = false;
			int year, month, day, hour, min, sec;
			year = month = day = hour = min = sec = 0;

			b = int.TryParse(text.Substring(0, 4), out year);
			b = int.TryParse(text.Substring(4, 2), out month);
			b = int.TryParse(text.Substring(6, 2), out day);
			b = int.TryParse(text.Substring(8, 2), out hour);
			b = int.TryParse(text.Substring(10, 2), out min);
			b = int.TryParse(text.Substring(12, 2), out sec);

			if(b) {
				return new DateTime(year, month, day, hour, min, sec);
			}
			else {
				return DateTimeHelper.Default;
			}
		}

		/// <summary>
		/// Fill a leading nill to a string
		/// </summary>
		/// <param name="number">The number.</param>
		/// <returns></returns>
		public static string AddLeadingNil(string number) {
			return number.PadLeft(2, '0');
		}

		/// <summary>
		/// Convert a string value on a save way into a boolean value
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static MailContainer ConvertOutlookMail(string value) {
			return Converter.ConvertOutlookMail(value, "(");
		}

		/// <summary>
		/// Convert a string value on a save way into a boolean value
		/// </summary>
		/// <param name="value">The value.</param>
		/// <param name="delimiter">The delimiter.</param>
		/// <returns></returns>
		public static MailContainer ConvertOutlookMail(string value, string delimiter) {
			if(value != null && value.Trim() != "") {
				MailContainer mail = new MailContainer();

				try {
					mail.Name = value.Substring(0, value.IndexOf(delimiter)).Trim();
				}
				catch(Exception) {
					mail.Name = "";
				}

				try {
					mail.Mail = WebValidator.CleanEmailString(value.Substring(
						value.IndexOf(delimiter),
						value.Length - value.IndexOf(delimiter)
					)).Trim();
				}
				catch(Exception) {
					mail.Mail = "";
				}

				return mail;
			}
			else {
				return new MailContainer("", "");
			}
		}
	}
}
